﻿'  __ _                 
' / _| |                
'| |_| |_   _ _ __ _ __ 
'|  _| | | | | '__| '__|
'| | | | |_| | |  | |   
'|_| |_|\__,_|_|  |_|

'Copyright (C) 2012, Rafal Marek Chmiel
'All rights reserved. http://flurr.codeplex.com/

'This file is part of the TwitterVB software
'Copyright (C) 2009, Duane Roelands
'All rights reserved.

'TwitterVB is a port of the Twitterizer library (http://code.google.com/p/twitterizer/)
'Copyright (C) 2008, Patrick "Ricky" Smith (ricky@digitally-born.com)
'All rights reserved.

' Redistribution and use in source and binary forms, with or without modification, are 
' permitted provided that the following conditions are met:
'
' - Redistributions of source code must retain the above copyright notice, this list 
'   of conditions and the following disclaimer.
' - Redistributions in binary form must reproduce the above copyright notice, this list 
'   of conditions and the following disclaimer in the documentation and/or other 
'   materials provided with the distribution.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
' ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
' IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
' INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
' NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
' PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
' ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
' POSSIBILITY OF SUCH DAMAGE.

Imports System.Text
Imports System
Imports System.Security.Cryptography
Imports System.Collections.Specialized
Imports System.IO
Imports System.Web
Imports System.Net

Namespace twitter

    Public Class twitterATwitterxAuth
	
        Inherits twitterOAuth2
		
        Public Const X_AUTHENTICATE As String = "https://api.twitter.com/oauth/access_token"
        Protected Const xAuthUserNamePrefix As String = "x_auth_username"
        Protected Const xAuthUserPasswordPrefix As String = "x_auth_password"
        Protected Const xAuthUserModePrefix As String = "x_auth_mode"

        'Note, in order to keep with Twitter's TOS, as soon as we get the tokens back we need to delete these out

        Public xUserName As String = String.Empty
        Public xUserPass As String = String.Empty

        Public Sub New()
		
        End Sub

        Public Sub New(ByVal p_ConsumerKey As String, ByVal p_ConsumerKeySecret As String)
            Me.ConsumerKey = p_ConsumerKey
            Me.ConsumerSecret = p_ConsumerKeySecret

        End Sub

        Public Sub New(ByVal p_ConsumerKey As String, ByVal p_ConsumerSecret As String, ByVal p_UserName As String, ByVal p_Password As String)
            Me.ConsumerKey = p_ConsumerKey
            Me.ConsumerSecret = p_ConsumerSecret
            Me.xUserName = p_UserName
            Me.xUserPass = p_Password
        End Sub

        Public Overloads Sub GetXAccess()
            Call Me.GetXAccess(Me.xUserName, Me.xUserPass)
        End Sub

        Public Overloads Sub GetXAccess(ByVal p_strUserName As String, ByVal p_strPassword As String)
            Dim Response As String = OAuthWebRequest(Method.POST, X_AUTHENTICATE, "?x_auth_username=" & OAuthUrlEncode(p_strUserName) & "&x_auth_password=" & OAuthUrlEncode(p_strPassword) & "&x_auth_mode=client_mode")
            If Response.Length > 0 Then
                Dim qs As NameValueCollection = HttpUtility.ParseQueryString(Response)
                If qs("oauth_token") IsNot Nothing Then
                    Token = qs("oauth_token")
                End If
                If qs("oauth_token_secret") IsNot Nothing Then
                    TokenSecret = qs("oauth_token_secret")
                End If
            End If
        End Sub

    End Class
	
End Namespace